package org.kjtc.controller;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.kjtc.balanceService.MeterRecordService;
import org.kjtc.entity.DeviceObject;
import org.kjtc.util.Const;
import org.kjtc.util.FileUpload;
import org.kjtc.util.Tools;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

@Controller
public class ChargingStationRequestController extends BaseController {

	@Resource(name = "deviceObject")
    private DeviceObject deviceObject;

	@Resource(name = "meterRecordService")
	private MeterRecordService meterRecordService;

	@ResponseBody
	@RequestMapping(value = "/meterRecord/request")
	public Object getStationMeterRecordRequest(HttpServletRequest request, @RequestBody String requestBody) {

		Map<String, Object> map = new HashMap<String, Object>();
		JSONObject json = JSONObject.fromObject(requestBody);
		JSONArray meterRecordJsonArray = Tools.getJsonArrayData(json, "chargingRecordData");
		if (meterRecordJsonArray != null) {
			try {
				int count = meterRecordService.saveMeterRecord(meterRecordJsonArray, null);
				logger.info("保存抄表记录" + count + "条");
				map.put("result", "Success");
			} catch (Exception e) {
				map.put("result", "Failed");
				e.printStackTrace();
				logger.error("保存抄表记录异常：" + e.getMessage());
			}
		}
		map.put("result", "Success");
		String jsonResult = JSONObject.fromObject(map).toString();
		logger.info("云平台接收子站上传抄表返回数据：" + jsonResult);
		map = null;
		return jsonResult;
	}

	@ResponseBody
	@RequestMapping(value = "/meterImg/request")
	public Object getStationMeterImgRequest(@RequestParam(value="uploadFiles", required=false) MultipartFile file, String meterTime, String chargingStationID, String meterCode, String meterType) throws IOException {

		Map<String, Object> map = new HashMap<String, Object>();
		String [] meterTimeArr = meterTime.split("[^\\d]");
		String filePath = Const.BALANCE_PATH.concat("meterrecord").concat(File.separator)
				.concat(meterTimeArr[0]).concat(File.separator).concat(meterTimeArr[1])
				.concat(File.separator).concat(meterTimeArr[2]).concat(File.separator)
				.concat(chargingStationID).concat(File.separator);
		String meterTimeAfter = meterTimeArr[0] + meterTimeArr[1] + meterTimeArr[2] + meterTimeArr[3] + meterTimeArr[4] + meterTimeArr[5];
		String fileName = "";
		switch (Integer.parseInt(meterType)) {
		case 0:
			fileName = chargingStationID.concat(meterTimeAfter).concat("date").concat(meterCode).concat(".png");
			break;
		case 1:
			fileName = chargingStationID.concat(meterTimeAfter).concat("time").concat(meterCode).concat(".png");
			break;
		case 2:
			fileName = chargingStationID.concat(meterTimeAfter).concat("total").concat(meterCode).concat(".png");
			break;
		case 3:
			fileName = chargingStationID.concat(meterTimeAfter).concat("sharp").concat(meterCode).concat(".png");
			break;
		case 4:
			fileName = chargingStationID.concat(meterTimeAfter).concat("peak").concat(meterCode).concat(".png");
			break;
		case 5:
			fileName = chargingStationID.concat(meterTimeAfter).concat("flat").concat(meterCode).concat(".png");
			break;
		case 6:
			fileName = chargingStationID.concat(meterTimeAfter).concat("valley").concat(meterCode).concat(".png");
			break;
		case 7:
			fileName = chargingStationID.concat(meterTimeAfter).concat("idle").concat(meterCode).concat(".png");
			break;
		default:
			break;
		}
		if (!"".equals(fileName)) {
			FileUpload.upload(file, filePath, fileName, null);
			map.put("result", "Success");
			String jsonResult = JSONObject.fromObject(map).toString();
			logger.info("云平台接收子站上传抄表返回数据：" + jsonResult);
			map = null;
			return jsonResult;
		} else {
			return "";
		}
	}
}